package cn.jn.lhm.netty.part06;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import lombok.val;

import static cn.jn.lhm.util.ByteBufUtils.log;

/**
 * @author lhm 2753914576@qq.com
 * @time 2023/7/19 - 21:46
 **/
public class TestByteBufComposite {
    public static void main(String[] args) {
        val buf1 = ByteBufAllocator.DEFAULT.buffer().writeBytes(new byte[]{1, 2, 3, 4, 5});

        val buf2 = ByteBufAllocator.DEFAULT.buffer().writeBytes(new byte[]{6, 7, 8, 9, 10});

        buf1.markReaderIndex();
        buf2.markReaderIndex();

        // 会发生数据拷贝
        ByteBuf byteBuf = ByteBufAllocator.DEFAULT.buffer().writeBytes(buf1).writeBytes(buf2);
        log(byteBuf);

        buf1.resetReaderIndex();
        buf2.resetReaderIndex();

        // 不会发生数据的拷贝
        ByteBuf byteBuf1 = ByteBufAllocator.DEFAULT.compositeBuffer().addComponents(true, buf1, buf2);
        log(byteBuf1);
    }
}
